import type { RouteRecordRaw } from 'vue-router'
import router from '../router/index'
type IRoute = RouteRecordRaw & { id?: string } | any
const allRoutes: IRoute[] = [
    {
        id: '2-1',
        name: "shoplist",
        path: 'shoplist',
        component: () => import("../views/admin/shop/shoplist.vue")
    }, {
        id: '2-2',
        name: "addshop",
        path: 'addshop',
        component: () => import("../views/admin/shop/addshop.vue")
    }, {
        id: '2-3',
        name: "tuijianshop",
        path: 'tuijianshop',
        component: () => import("../views/admin/shop/tuijianshop.vue")
    }, , {
        id: '3-1',
        name: "list",
        path: 'list',
        component: () => import("../views/admin/banner/list.vue")
    }, {
        id: '3-2',
        name: "add",
        path: 'add',
        component: () => import("../views/admin/banner/add.vue")
    }, {
        id: '4-1',
        name: 'adminlist',
        path: 'adminlist',
        component: () => import("../views/admin/admin/adminlist.vue")
    }, {
        id: '4-2',
        name: 'addadmin',
        path: 'addadmin',
        component: () => import("../views/admin/admin/addadmin.vue")
    }, {
        id: '6-1',
        name: 'dingdanlist',
        path: 'dingdanlist',
        component: () => import("../views/admin/dingdan/dingdanlist.vue")
    }, {
        id: '6-2',
        name: 'changecity',
        path: 'changecity',
        component: () => import("../views/admin/dingdan/changecity.vue")
    }
    , {
        id: '7-1',
        name: 'dizhi',
        path: 'dizhi',
        component: () => import("../views/admin/dizhi/dizhi.vue")
    }, {
        id: '8-1',
        name: 'shujushow',
        path: 'shujushow',
        component: () => import("../views/admin/shujushow/kline.vue")
    }, {
        id: '8-2',
        name: 'shujushowz',
        path: 'shujushowz',
        component: () => import("../views/admin/shujushow/zline.vue")
    }]
export default function getRoutes() {
    let checkedKeys: string[] = []
    const grants = sessionStorage.getItem('grants');

    if (grants) {
        checkedKeys = JSON.parse(grants)
    } if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '2-3', '3', '3-1', "3-1-R", "3-1-U", "3-1-D", '3-2', '4', '4-1', '4-2', '5', '5-1', '6', '6-1', "6-2", "7", "7-1", '8', '8-1', '8-2']
    }

    allRoutes.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            let currRoute = { ...item }

            let arr = ["0", "0", "0", "0"];
            if (checkedKeys.includes(item.id + "-C")) {
                arr[0] = "1";
            }
            if (checkedKeys.includes(item.id + "-R")) {
                arr[1] = "1";
            }
            if (checkedKeys.includes(item.id + "-U")) {
                arr[2] = "1";
            }
            if (checkedKeys.includes(item.id + "-D")) {
                arr[3] = "1";
            }

            currRoute.meta = {
                crud: arr.join("")
            }
            router.addRoute("home", currRoute);
        }
    })
    console.log("router.getRoutes()", router.getRoutes())
}

export const clearCurrentRoutes = () => {
    let checkedKeys: string[] = [];
    const grants = sessionStorage.getItem("grants")
    if (grants) {
        checkedKeys = JSON.parse(grants)
    } if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '2-3', '3', '3-1', '3-2', '4', '4-1', '4-2', '5', '5-1', '6', '6-1', '7', '7-1']
    }
    allRoutes.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            router.removeRoute(item.name as string);
        }
    })
}